package people.paper.tv.activity.home.modual.ywyl.view.impl;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.skyworth.ui.api.SkyLogo;
import com.skyworth.ui.newrecycleview.NewRecycleAdapterItem;
import com.skyworth.util.Util;
import com.skyworth.util.imageloader.ImageLoader;

import people.paper.tv.R;
import people.paper.tv.activity.home.modual.ywyl.view.data.NewsData;

/**
 * Created by sunny on 16-9-9.
 */
public class FocusNewsItemView extends FrameLayout implements NewRecycleAdapterItem<NewsData> {
    String TAG = "HomeThemeItemView";
    Context mContext;
    FrameLayout itemLayout;
    int imgWidth = Util.Div(414), imgHeight = Util.Div(736);
    NewsData newsData;
    int curPosition;
    View img, mFocusView;
    String mUrl;

    public FocusNewsItemView(Context mContext) {
        super(mContext);
        this.mContext = mContext;

        setFocusable(true);
        setFocusableInTouchMode(true);
        setClipChildren(false);
        setClipToPadding(false);

        this.setLayoutParams(new LayoutParams(imgWidth, imgHeight));
        itemLayout = new FrameLayout(mContext);
        LayoutParams itemParams = new LayoutParams(imgWidth,imgHeight);
        addView(itemLayout,itemParams);
        itemLayout.setClipChildren(false);
        itemLayout.setClipToPadding(false);

        mFocusView = new View(getContext());
        mFocusView = new ImageView(mContext);
        mFocusView.setBackgroundResource(R.mipmap.block_focus);
        int focusW = Util.Div(30)+imgWidth;
        int focusH = Util.Div(98)+imgHeight;
        LayoutParams focusParams = new LayoutParams(focusW, focusH);
        focusParams.topMargin = Util.Div(-20);
        focusParams.leftMargin = Util.Div(-16);
        itemLayout.addView(mFocusView,focusParams);
        mFocusView.setVisibility(GONE);

        img = ImageLoader.getLoader().getView(this.mContext);
        itemLayout.addView(img, new LayoutParams(imgWidth, imgHeight));
    }

    @Override
    public void refreshUI() {
        Log.i("FocusNewsItemView", "sunny update:" + newsData);
        if (newsData != null) {
            update(newsData);
        } else {
            beforeUpdate();
        }
    }

    public void update(NewsData mdata) {
//        Log.i("FocusNewsItemView", "sunny update:" + mdata.imgUrl);
        if(newsData != null && mUrl != null)
            Log.i("tag", "focusnew update:" + mUrl+"; "+mUrl.equals(mdata.imgUrl)+"; "+mdata.title+"; "+mdata.imgUrl);
        if(mUrl == null || !mUrl.equals(mdata.imgUrl))
            ImageLoader.getLoader().with(getContext()).resize(imgWidth, imgHeight)
                    .setScaleType(ImageView.ScaleType.CENTER_CROP).setPlaceHolder(
                    SkyLogo.getInstence().getLogoRes(false)).load(Uri.parse(mdata.imgUrl)).into(img);
        mUrl = mdata.imgUrl;
    }

    private void beforeUpdate() {
        if(newsData != null && mUrl != null)
            Log.i("tag", "focusnew beforeUpdate:" + mUrl + "; "+newsData.title);
        if (img != null)
            ImageLoader.getLoader().reset(img);
        if(mUrl != null)
            ImageLoader.getLoader().clearCacheFromMemory(mUrl);
        mUrl = null;
    }

    public void onFocusChange(boolean hasFocus) {
        mFocusView.bringToFront();
        mFocusView.setVisibility(hasFocus ? View.VISIBLE : View.INVISIBLE);
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN_MR1){
            Util.focusAnimate(this, hasFocus);
        }
    }

    public void onClickAction() {
        if(newsData != null)
            newsData.onClick.run();
    }

    @Override
    public void onUpdateData(NewsData data, int position) {
        this.newsData = data;
        curPosition = position;
        if (data == null) {
            return;
        }
        refreshUI();
    }

    @Override
    public void clearItem() {
        beforeUpdate();
    }

    public void resume() {
        if(newsData != null)
            Log.i("tag", "focusnew resume:" + mUrl+"; "+newsData.title);
        if(mUrl != null && img != null){
            ImageLoader.getLoader().with(getContext()).resize(imgWidth, imgHeight)
                    .setScaleType(ImageView.ScaleType.CENTER_CROP).setPlaceHolder(
                    SkyLogo.getInstence().getLogoRes(false)).load(Uri.parse(mUrl)).into(img);
            ImageLoader.getLoader().startAnimation(img);
        }
    }

    public void pause() {
        if(newsData != null)
            Log.i("tag", "focusnew pause:" + mUrl+"; "+newsData.title);
        if (img != null)
            ImageLoader.getLoader().reset(img);
        if(mUrl != null)
            ImageLoader.getLoader().clearCacheFromMemory(mUrl);
    }

    @Override
    public void destroy() {
        if (img != null)
            ImageLoader.getLoader().reset(img);
        if(mUrl != null)
            ImageLoader.getLoader().clearCacheFromMemory(mUrl);
        newsData = null;
    }

    @Override
    public View getView() {
        return this;
    }

}
